using System.Collections;
using UnityEngine;

public class TapControl : MonoBehaviour, MoveDirection
{
	private Vector2 direction = Vector2.zero;

	private int layerMask;

	private Vector2 currentTarget = Vector2.zero;

	private bool canControl;

	[SerializeField]
	private Transform wayTrajectory;

	[SerializeField]
	private SpriteRenderer[] directionSprites;

	[SerializeField]
	private Color maxDistanceColor;

	[SerializeField]
	private Color minDistanceColor;

	[SerializeField]
	private GameObject _guide;

	public Transform _headPos;

	public bool tapControlChosen;

	public bool valid;

	public bool pressed;

	public Vector2 Direction => direction;

	public void StartControl()
	{
		canControl = true;
	}

	public void StopControl()
	{
		canControl = false;
		currentTarget = Vector2.zero;
		direction = Vector2.zero;
		wayTrajectory.localPosition = Vector3.zero;
	}

	private void Start()
	{
		_guide.SetActive(value: false);
		layerMask = LayerMask.GetMask("TapPlane");
		if (Preloader.TapControlConfig.IsEnabled && NintendoPlayerPrefs.GetInt("First", 0) == 0)
		{
			_guide.SetActive(value: true);
			StartCoroutine(KillGuide());
		}
		IEnumerator KillGuide()
		{
			while (direction == Vector2.zero)
			{
				yield return new WaitForFixedUpdate();
			}
			yield return new WaitForSeconds(1.5f);
			_guide.SetActive(value: false);
		}
	}

	private void FixedUpdate()
	{
		if (!_headPos || !canControl || !tapControlChosen || !valid)
		{
			return;
		}
		if (pressed && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out var hitInfo, 50f, layerMask))
		{
			currentTarget = new Vector2(hitInfo.point.x, hitInfo.point.y);
		}
		if (!(currentTarget == Vector2.zero))
		{
			Vector2 vector = new Vector2(_headPos.position.x, _headPos.position.y);
			Vector2 vector2 = currentTarget - vector;
			float num = vector2.magnitude;
			direction = vector2 / num;
			if (Vector2.Distance(vector, currentTarget) < 0.05f)
			{
				direction = Vector2.zero;
			}
			wayTrajectory.position = currentTarget;
			wayTrajectory.LookAt(_headPos);
			wayTrajectory.localScale = new Vector3(1f, 1f, num);
			wayTrajectory.GetChild(0).localPosition = new Vector3((wayTrajectory.position.x < _headPos.position.x) ? 0.42f : (-0.42f), 0f, 0.5f);
			if (num > 1f)
			{
				num = 1f;
			}
			directionSprites[0].color = Color.Lerp(minDistanceColor, maxDistanceColor, num);
			directionSprites[1].color = Color.Lerp(minDistanceColor, maxDistanceColor, num);
		}
	}
}
